const fp = require("lodash/fp");
const _ = require("lodash");
const { Maybe, Container } = require("./3-support.js");
let maybe = Maybe.of([5, 6, 1]);
let xs = Container.of(["do", "ray", "me", "fa", "so"]);

const log = console.log;

// 代码题 第三题 第1练习
let ex1 = (y) => {
  return maybe.map(fp.map(fp.add(y)));
};
ex1(1).map(log); // [ 6, 7, 2 ]
ex1(2).map(log); // [ 7, 8, 3 ]

// 代码题 第三题 第2练习
let ex2 = () => {
  return xs.map(fp.first);
};

ex2().map(log);

// 代码题 第三题 第3练习
let safeProp = fp.curry(function(x, o) {
  return Maybe.of(o[x]);
});

let user = { id: 2, name: "Albert" };
let ex3 = (data) => {
  return safeProp("name", data).map(fp.first);
};
ex3(user).map(log); // A

// 代码题 第三题 第4练习
let ex4 = function(n) {
  // if (n) {
  //   return parseInt(n);
  // }
  let maybe = Maybe.of(n);
  return maybe.map(fp.parseInt(10));
};
ex4("123").map(log);
